Blog

Validation - Enforcing minimum and maximum dates with the date picker control

When building data entry screens, it's often necessary to validate dates. Unfortunately, the date picker control contains no built-in way to specify the minimum and maximum permissible values, or other validation rules. This post describes a way to restrict the value that a user can enter through a date picker control.

A frequent question that arises is -  how can I restrict the value that a user can enter through a date picker control?

There is no built in way to define a minimum or maximum permissible date, or to exclude future dates or weekends. Therefore, a common workaround is to permit the entry of any date, but to add warning labels or apply validation on save buttons to prevent a user from using the date. The post beneath describes this technique.


Another workaround is to add formula to the OnChange property of the date picker control that checks whether the input date is valid. If the user enters an invalid date, the app notifies the user and resets the date picker control to the default value. The rest of this post describes how to define a maximum and minimum dates, and how to add validation that compares the input date against another field value.

Date picker - How to set a maximum date (eg Date cannot be greater than today)

To demonstrate how to enforce a maximum date, let's take the example where a user must not enter a 'create date' that is greater than today. To apply this rule, we can apply the following formula to the OnChange property of the date picker control.

If(Self.SelectedDate > Today() ,
Notify("Create date can not be greater than today",
NotificationType.Error);
Reset(Self)
)


If a user enters a create date greater than today, the app notifies the user and resets the date picker control back to the default value.

Date picker - How to set a minimum date (eg Date cannot be greater than today)

We can apply the same technique to enforce a minimum date. For example, we can apply the following formula to the OnChange property of a 'target close date' date picker control. This formula ensures that a user must enter a target close date in the future.

If(Self.SelectedDate < Today() ,
Notify("Target close date can not be less than today",
NotificationType.Error);
Reset(Self)
)

Note that for data entry forms, we can apply a variation of this formula to apply the rule only when a user creates a new record. This can better support scenarios where we want users at a future time to correct mistakes with existing records.
If(Self.SelectedDate < Today() And (EditForm1.Mode = FormMode.New),
Notify("Target close date can not be less than today",
NotificationType.Error);
Reset(Self)
)

Comparing two dates (eg close date cannot be greater than create date)

Another common scenario is to compare an input date against another field value. As an example, we can add the following formula to the OnChange property of a 'close date' date picker control. This formula ensures that a user must enter a 'close date' that is later than the 'create date'.

If(Self.SelectedDate < datePickerCreateDate.SelectedDate,
Notify("Close date can not be earlier than create date",
NotificationType.Error);
Reset(Self)
)

Conclusion

With the date picker control, there's no native way to set minimum and maximum values. This post described one workaround, which is to add formula to the OnChange property to discards the value if the user enters a date outside of the acceptable range.